package com.example.zhihu;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.zhihu.adapter.VpZhihuAdapter;
import com.example.zhihu.fragments.DailyNewsFragment;
import com.example.zhihu.fragments.HotFragment;
import com.example.zhihu.fragments.SectionsFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private TabLayout mMainTab;
    private ArrayList<Fragment> mFragments;
    private ArrayList<String> mTitles;
    private ViewPager mMainVp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        mMainTab = findViewById(R.id.main_tab);
        mMainVp = findViewById(R.id.main_vp);
        initTitles();
        initFragments();

        VpZhihuAdapter adapter = new VpZhihuAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mMainVp.setAdapter(adapter);
        mMainTab.setupWithViewPager(mMainVp);
    }

    private void initFragments() {
        mFragments = new ArrayList<>();
        mFragments.add(new DailyNewsFragment());
        mFragments.add(new SectionsFragment());
        mFragments.add(new HotFragment());
    }

    private void initTitles() {
        mTitles = new ArrayList<>();
        mTitles.add("日报");
        mTitles.add("专栏");
        mTitles.add("热门");
    }
}
